package imp;

import dao.CommentsDao;
import model.Comments;
import util.DUBtil;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Comments_imp implements CommentsDao {

    @Override
    public boolean AddComments(Comments comments) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        String sql = "insert into comments(id,userid,userimgurl,username,goodsid,date,content) values(?,?,?,?,?,?,?)";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, comments.getId());
            pstmt.setString(2, comments.getUserId());
            pstmt.setString(3, comments.getUserImgUrl());
            pstmt.setString(4, comments.getUserName());
            pstmt.setString(5, comments.getGoodsId());
            pstmt.setString(6, comments.getDate());
            pstmt.setString(7, comments.getContent());
            pstmt.executeUpdate();
            con.close();
            pstmt.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Comments> ShowComments(String goodsid) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select com.date,com.content,u.imgurl,u.name username,u.id userid\n" +
                "from comments com,user u\n" +
                "where com.userid=u.id and goodsid=?\n" +
                "ORDER BY date DESC";
        List<Comments> list = new ArrayList<>();
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, goodsid);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Comments comments = new Comments();
                comments.setUserImgUrl(rs.getString("imgurl"));
                comments.setUserName(rs.getString("username"));
                comments.setUserId(rs.getString("userid"));
                comments.setDate(rs.getString("date"));
                comments.setContent(rs.getString("content"));
                System.out.println(comments.getId());
                System.out.println(comments.getDate());
                System.out.println(comments.getUserImgUrl());
                list.add(comments);
            }
            rs.close();
            pstmt.close();
            con.close();
            return list;
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,
                    "数据库访问失败");
            return null;
        }
    }

    @Override
    public boolean DeleteComments(String goodsid) {
        Connection connection = new DUBtil().getConnection();
        PreparedStatement pstml = null;
        String sql = "delete from comments where goodsid=?";
        try {
            pstml = connection.prepareStatement(sql);
            pstml.setString(1, goodsid);
            pstml.executeUpdate();
            connection.close();
            pstml.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
